﻿using UnityEngine;
using System.Collections;
//
//public partial class Clock : MonoBehaviour { // singleton
//	
//	private static Clock instance;  
//	private static GameObject container;  
//	public static Clock GetInstance()  
//	{  
//		if( !instance )  
//		{  
//			container = new GameObject();  
//			container.name = "Clock";  
//			instance = container.AddComponent(typeof(Clock)) as Clock;  
//		}  
//		return instance;  
//	}  
//	
//}

public partial class Clock : MonoBehaviour {

	public static int iYear = 1996;
	public static int iMonth = 11;
	public static string strMonth;
	public static int iDay = 3;
	public static int iHour = 7;

	float fTimer = 350.0f;
    public static float fElapseSpeed;

	void Start(){
		MonthSwitch ();
	}

	void Update(){
        fElapseSpeed = 15.0f * Time.deltaTime;  // 게임은 15로 // 디버그는 50
		fTimer += fElapseSpeed;
		if(iHour >= 24)
		{
			iHour = 0;
            HourChged();
			++iDay;

            if (iMonth == 11 && iDay == 4 && iHour == 0) // Ending Day
            {
                //GameSystem.GetInstance().GameEnding();
            }
		}
		
		if(iMonth == 2 && iDay >28)
		{
			++iMonth;
			iDay = 1;
		}else if((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay >30)
		{
			++iMonth;
			iDay = 1;
		}else if((iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7
		          || iMonth == 8 || iMonth == 10 || iMonth == 12) && iDay >31)
		{
			if(iMonth == 12)
			{
				iMonth = 1;
				iDay = 1;
				++iYear;
			}else{
				++iMonth;
				iDay = 1;
			}
		}
		
		if(iHour == 0){
			if(fTimer >1320.0f)  // 총크기 + 시작타이머
            {
				++iHour;
                HourChged();
            }
        }
        else if (fTimer > (iHour) * 55)
        {
            ++iHour;
            HourChged();
        }
		
		if(fTimer > 1320.0f)
		{
			fTimer = 0;
		}

		MonthSwitch ();
        GetComponent<UILabel>().text = string.Format("{0} {1}, {2}. {3} : 00", strMonth, iDay, iYear, iHour);
	}

    void HourChged()
    {
        if(iHour == 17)
            GameObject.Find("SunLogic").GetComponent<SunLogic>().SunSetRising(false);
        else if (iHour == 5)
        {
            GameObject.Find("SunLogic").GetComponent<SunLogic>().SunSetRising(true);
            GameObject.Find("SunLogic").GetComponent<SunLogic>().SunPosReset();
        }

        GameObject.Find("SunLogic").GetComponent<SunLogic>().WeatherCounting();
    }

	void MonthSwitch(){
		switch(iMonth)
		{
		case 1:
			strMonth = "Jan";
			break;
		case 2:
			strMonth = "Feb";
			break;
		case 3:
			strMonth = "Mar";
			break;
		case 4:
			strMonth = "Apr";
			break;
		case 5:
			strMonth = "May";
			break;
		case 6:
			strMonth = "Jun";
			break;
		case 7:
			strMonth = "Jul";
			break;
		case 8:
			strMonth = "Aug";
			break;
		case 9:
			strMonth = "Sep";
			break;
		case 10:
			strMonth = "Oct";
			break;
		case 11:
			strMonth = "Nov";
			break;
		case 12:
			strMonth = "Dec";
			break;
		}
	}
}